<?php
/**
 * Created by jinqiang on 2024/5/30 13:38.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Produce;


use App\Http\Controllers\Controller;
use App\Http\Services\Produce\FactoryService;
use Illuminate\Http\Request;

class FactoryController extends Controller
{
    //工厂搜索
    public function search(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'factory_name' => 'required'
        ]);
        $param = $request->all();
        $param['limit'] = 20;
        $res = $factoryService->search($param);
        $data = [];
        foreach ($res['data'] as $item) {
            $data[$item['supplier_id']] = $item['factory_name'];
        }
        return result($data);
    }

    public function list(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        $res = $factoryService->search($param);
        return result($res);
    }

    //工厂预排--列表
    public function preList(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required'
        ]);
        $param = $request->all();
        $res = $factoryService->searchPre($param);
        return result($res);
    }

    //加工厂预排--设置
    public function preSave(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'data' => 'required'
        ]);
        $param = $request->all();
        $res = $factoryService->savePre($param['data']);
        return $res['code'] === 0 ? success("设置成功", $res['data']) : error($res['msg'], $res);
    }

    //加工厂--启用、禁用
    public function open(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'id' => 'required',
            'status' => 'required'
        ]);
        $param = $request->all();
        $res = $factoryService->open($param);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //加工厂--推送
    public function push(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'supplier_id' => 'required',
        ]);
        $param = $request->all();
        $res = $factoryService->push($param['supplier_id']);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //加工厂--保存
    public function save(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'data' => 'required',
        ]);
        $param = $request->all();
        $res = $factoryService->save($param['data']);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //加工厂--开通E签宝
    public function openEqb(Request $request, FactoryService $factoryService)
    {
        $this->validate($request, [
            'supplier_id' => 'required',
            'type' => 'required|in:kz,yx'
        ]);
        $param = $request->all();
        $res = $factoryService->openEqb($param['supplier_id'], $param['type']);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }
}
